﻿/*Software Center*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using websitephuongthao.Datasql;
using System.Configuration;
using System.Data.SqlClient;

public partial class MasterPage : System.Web.UI.MasterPage
{
    //Dataconnection conn = new Dataconnection();
    SqlConnection conn = new SqlConnection("Data Source=localhost;Initial Catalog=GSPhuongThao;Persist Security Info=True;User ID=sa;Password=123456");
    SqlCommand cmd;
    DataTable dt;
    SqlDataAdapter da;

    protected void Page_Load(object sender, EventArgs e)
    {
        ltrSlide.Text = load_data();
        int quyen = Convert.ToInt32(Session["phanquyenId"]);
        int thanhvienId = Convert.ToInt32(Session["thanhvienId"]);
        string tenDN = (string)Session["tendangnhap"];
        if (Session["tendangnhap"] != null && quyen == 3)
        {
            lbltendangnhap.Text = tenDN;
            lbtndangky.Visible = false;
            lbtndangnhap.Visible = false;
            lbtnquantri.Visible = false;
            pnchao.Visible = true;
        }
        else if (Session["tendangnhap"] != null && quyen == 1 || (Session["tendangnhap"] != null && quyen == 2))
        {
            lbltendangnhap.Text = tenDN;
            lbtndangky.Visible = false;
            lbtndangnhap.Visible = false;
            lbtnquantri.Visible = true;
            pnchao.Visible = true;
        }
        else
        {
            lbtnquantri.Visible = false;
        }
    }
    protected void lbtndangnhap_Click(object sender, EventArgs e)
    {
        
    }
    protected void lbtndangxuat_Click(object sender, EventArgs e)
    {
        Session["tendangnhap"] = null;
        Session["thanhvienId"] = null;
        Session["phanquyenId"] = null;
        pnchao.Visible = false;
        
        Response.Redirect("Default.aspx");
    }

    protected String load_data()
    {
        String strHTML = "";
        int i = 1, j = 1;
        strHTML += "<div id=\"list_slide\">";

        dt = queryToTable("select top(5) * from TinHoatDong where trangthai=1 order by [tinhoatdongId] desc");
        if(dt.Rows.Count > 0)
        {
            
            foreach (DataRow dr2 in dt.Rows)
            {
                String urlAnh2 = dr2["anhhoatdong"].ToString();
                String url2 = urlAnh2.Substring(2);

                strHTML += @"
                    <div id='fragment-" + i.ToString() + @"' class='ui-tabs-panel'>
                        <a style='color: rgb(255, 255, 255);' href=NoiDungHoatDong.aspx?mbv=" + dr2["tinhoatdongId"] + @">
                            <img src='" + url2 + @"' width='412px' height='250px' />
                        </a>
                        <div class='info'>
                            <a href=NoiDungHoatDong.aspx?mbv=" + dr2["tinhoatdongId"] + @">" + dr2["tieude"] + @"</h2>
                            <p></p>
                        </div>
                    </div>";
                i++;
            }

            strHTML += "<ul class='ui-tabs-nav' >";
            foreach (DataRow dr in dt.Rows)
            {
                String urlAnh = dr["anhhoatdong"].ToString();
                String url = urlAnh.Substring(2);

                strHTML += @"
                <li class='ui-tabs-nav-item' id='nav-fragment-" + j.ToString() + @"'>
                    <a href='#fragment-" + j.ToString() + @"'/>
                        <img src='" + url + @"'/>
                        <span'>" + dr["tieude"] + @"</span>
                    </a>
                </li>";
                j++;
            }
            strHTML += @"
                    </ul>";
        }

        strHTML += "</div>";
        return strHTML;
    }

    private DataTable queryToTable(String query)
    {
        dt = new DataTable();
        try {
            conn.Open();
            da = new SqlDataAdapter(query, conn);
            DataSet ds = new DataSet();
            da.Fill(ds, "GetData");
            dt = ds.Tables[0];
        }
        catch (Exception e)
        {
 
        }
        return dt;
    }
}
